<template>
  <div>
    <!-- 销售退车查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhReturnQuery"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(4)">导出</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="emitvisible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :type="handleType" :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSeacher :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { vehicleConfiguration } from '@/api/omFinance';
import { exportExcelFile } from '@/utils';
import { queryListForFactory, exportList,exporturl } from '@/api/omSale/SalesReturn/ReturnQuery/index.js';
import SonModel from './SonModel';
import OwnerSeacher from '../OwnerSeacher';

export default {
  name: 'ReturnQuery',
  mixins: [dictionary],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      fetchApi: queryListForFactory,
      formData: {},
      emitvisible: false,
      isDealer: false,
      handleType: '',
      drawerTitle: '',
      isImport: false,
      dealerCode: ''
    };
  },
  components: {
    SonModel,
    OwnerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.dealerHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'vehicleInfo',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '零售订单号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyStartDate|applyEndDate',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '实销退车单号',
          fieldName: 'salesReturnNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '退车状态',
          fieldName: 'applyStatus',
          placeholder: '请选择',
          itemList: this.createDictList('4523')
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '退车单状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4523'),
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '4523')}</span>;
          }
        },
        {
          title: '实销退车单号',
          dataIndex: 'returnNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '零售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '出库日期',
          dataIndex: 'stockDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '实销日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车类型',
          dataIndex: 'returnType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4522'),
          render: p => {
            return <span>{this.createDictText(p.row.returnType, '4522')}</span>;
          }
        },
        {
          title: '是否质量问题',
          dataIndex: 'isQualityProblem',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('4521'),
          render: p => {
            return <span>{this.createDictText(p.row.isQualityProblem, '4521')}</span>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '提报日期',
          dataIndex: 'submitDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '确认日期',
          dataIndex: 'confirmDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '入库日期',
          dataIndex: 'storageDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '退车说明',
          dataIndex: 'returnDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {},
    seacherFun() {
      this.isowner = true;
    },
    async addHandle(type) {
        let json = Object.assign({}, { CODE: 'CODE_0000053' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     exportList(json)
      //       .then(response => {
      //         exportExcelFile(response.data, '销售退车查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     console.log('456');
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data, type) {
      this.formData = data;
      if (type == 1) {
        this.drawerTitle = '编辑';
        this.handleType = 'emit';
        this.emitvisible = true;
      } else {
        this.drawerTitle = '明细';
        this.handleType = 'show';
        this.emitvisible = true;
      }
    },
    emitcloseHandler() {
      this.emitvisible = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    dealerHandle() {
      console.log('---经销商---');
      this.isDealer = true;
    },
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.dealerCode = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    importcloseHandler() {
      this.isImport = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
